package com.ydd.controller.user;

import com.ydd.exception.BaseException;
import com.ydd.result.Result;
import com.ydd.vo.UploadVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-05-31 20:42
 */

@Api(tags = "文件上传相关接口")
@RestController
@Slf4j
@RequestMapping
public class CommonController {

    @ApiOperation("后文件上传接口")
    @PostMapping("/admin/common/upload")
    public Result upload(MultipartFile file, String type) throws IOException {
        log.info("文件上传：{},{}",file,type);
        Long size = file.getSize();
        //改成优点达后端/html/dist路径
        String backendSystemPath = "E:\\nginx\\nginx-ydd-backend\\html\\dist\\";

        //获取原始文件名
        String originalFilename = file.getOriginalFilename();//xxxx.jpg
        //截取文件的扩展名
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));//.jpg
        //生成唯一文件名
        String fileName = UUID.randomUUID().toString().replace("-", "") + extName;
        //生成日期时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        //生成后端传输路径
        String backendPath = "undefinedupload/" + type + "/" + format + "/" + fileName;

        File dest = new File(backendSystemPath +backendPath);

        if (!dest.exists()) {
            dest.mkdirs();
        }

        file.transferTo(dest);

        //后端ng的静态文件再传一份去前端ng
        String frontendSystemPath = "E:\\nginx\\nginx-ydd-front\\html\\dist\\";
        String frontPath = "apiupload/" + type + "/" + format + "/" + fileName;

        File sourceFile = new File(backendSystemPath+backendPath);
        File targetFile = new File(frontendSystemPath+frontPath);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.flush();
        bis.close();
        bos.close();

        return Result.success(
                UploadVO.builder()
                        .savePath(backendPath)
                        .name(fileName)
                        .mime(type)
                        .size(size)
                        .build()
        );
    }

    @ApiOperation("后台文件上传接口")
    @PostMapping("/api/common/upload")
    public Result apiUpload(MultipartFile file, String type) throws IOException {
        log.info("文件上传：{},{}",file,type);
        Long size = file.getSize();
        //改成优点达后端/html/dist路径
        String systemPath = "E:\\nginx\\nginx-ydd-front\\html\\dist\\api";
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();//xxxx.jpg
        //截取文件的扩展名
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));//.jpg
        //生成唯一文件名
        String fileName = UUID.randomUUID().toString().replace("-", "") + extName;
        //生成日期时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        //生成传输路径
        String path = "upload/" + type + "/" + format + "/" + fileName;

        File dest = new File(systemPath+path);

        if (!dest.exists()) {
            dest.mkdirs();
        }

        file.transferTo(dest);

        String backendSystemPath = "E:\\nginx\\nginx-ydd-backend\\html\\dist\\";
        String backendPath = "undefinedupload/" + type + "/" + format + "/" + fileName;

        File sourceFile = new File(systemPath+path);
        File targetFile = new File(backendSystemPath+backendPath);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.flush();
        bis.close();
        bos.close();


        return Result.success(
                UploadVO.builder()
                        .savePath(path)
                        .name(fileName)
                        .mime(type)
                        .size(size)
                        .build()
        );
    }
}